package com.sojson.project.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.controller.param.verify.GoodSpecsParamVerify;
import com.sojson.project.entity.po.GoodSpecs;
import com.sojson.project.service.GoodSpecsService;
import com.sojson.result.Result;

import io.swagger.annotations.Api;

/**
 * 商品规格组合表控制器
 * 
 * @author liu
 * @date 2021-03-22
 */
@Api(tags = "商品规格组合表管理")
@RestController
@RequestMapping(value = "/goodSpecs")
public class GoodSpecsController {

    @Resource
    private GoodSpecsService goodSpecsServiceImpl;

    /**
     * 单条插入
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/insert')")
    public Object insert(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.insert(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goodSpecsServiceImpl.insert(goodSpecs);
        return Result.success();
    }

    /**
     * 单条插入
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PostMapping("/insertOne")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/insert')")
    public Object insertOne(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.insertOne(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        goodSpecs.insertInit();
        goodSpecsServiceImpl.insertOne(goodSpecs);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.insertList(goodSpecs));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.deleteByDto(goodSpecs));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/countAll")
    public Object countAll() throws SQLException {
        int countAll = goodSpecsServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findAll")
    public Object findAll() throws SQLException {
        List<GoodSpecs> findAll = goodSpecsServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/countByDto")
    public Object countByDto(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        int countByDto = goodSpecsServiceImpl.countByDto(goodSpecs);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/findByDto')")
    public Object findByDto(GoodSpecs goodSpecs) throws SQLException {
        List<GoodSpecs> findByDto = goodSpecsServiceImpl.findByDto(goodSpecs);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @PostMapping("/deleteById")
    public Object deleteById(Integer id) throws SQLException {
        goodSpecsServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteByIdList/{ids}")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/deleteByIdList/{ids}')")
    public Object deleteByIdList(@PathVariable List<Integer> ids) throws SQLException {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(goodSpecsServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateById")
    public Object updateById(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        goodSpecsServiceImpl.updateById(goodSpecs);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.updateStaticByIdList(goodSpecs));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.updateChangeByIdList(goodSpecs));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/updatePartialById')")
    public Object updatePartialById(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.updatePartialById(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        goodSpecs.updateInit();
        goodSpecs.setUpdateNull();
        goodSpecsServiceImpl.updatePartialById(goodSpecs);
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateOneById")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/updatePartialById')")
    public Object updateOneById(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.updateOneById(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        goodSpecs.updateInit();
        goodSpecs.setUpdateNull();
        goodSpecsServiceImpl.updatePartialById(goodSpecs);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.updateStaticPartialByIdList(goodSpecs));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        return Result.success(goodSpecsServiceImpl.updateChangePartialByIdList(goodSpecs));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @GetMapping(value = "/findById")
    public Object findById(String id) throws SQLException {
        GoodSpecs findById = goodSpecsServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) throws SQLException {
        ArrayList<GoodSpecs> findByIdList = goodSpecsServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    // @PostMapping("/findIdByDto")
    public Object findIdByDto(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        ArrayList<Integer> findIdByDto = goodSpecsServiceImpl.findIdByDto(goodSpecs);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @PostMapping("/clear")
    public Object clear() throws SQLException {
        goodSpecsServiceImpl.clear();
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)(启用,禁用)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateStatusById")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/updateStatusById')")
    public Object updateStatusById(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.updateStatusById(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        GoodSpecs goodSpe = new GoodSpecs();
        goodSpe.updateInit();
        goodSpe.setId(goodSpecs.getId());
        goodSpe.setStatus(goodSpecs.getStatus());
        goodSpecsServiceImpl.updatePartialById(goodSpe);
        return Result.success();
    }

    /**
     * 根据对象查询(包含规格详细)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/list')")
    public Object list(GoodSpecs goodSpecs) throws SQLException {
        List<GoodSpecs> findByDto = goodSpecsServiceImpl.list(goodSpecs);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id查询(包含规格详细)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/getInfo")
    public Object getInfo(Integer id) throws SQLException {
        GoodSpecs getInfo = goodSpecsServiceImpl.getInfo(id);
        return Result.success(getInfo);
    }

    /**
     * 添加库存
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/addStock")
    @PreAuthorize("@ss.hasPermi('/goodSpecs/addStock')")
    public Object addStock(@RequestBody GoodSpecs goodSpecs) throws SQLException {
        // 校验参数
        Result<?> paramResult = GoodSpecsParamVerify.addStock(goodSpecs);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(goodSpecsServiceImpl.updateStock(goodSpecs));
    }

    /**
     * 根据对象查询
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findAppByDto")
    public Object findAppByDto(GoodSpecs goodSpecs) throws SQLException {
        goodSpecs.setStatus(EGeneralStatus.NORMAL.getCode());
        List<GoodSpecs> findByDto = goodSpecsServiceImpl.findByDto(goodSpecs);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询(包含规格详细)
     * 
     * @param goodSpecs
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/listApp")
    public Object listApp(GoodSpecs goodSpecs) throws SQLException {
        goodSpecs.setStatus(EGeneralStatus.NORMAL.getCode());
        List<GoodSpecs> findByDto = goodSpecsServiceImpl.list(goodSpecs);
        return Result.success(findByDto, findByDto);
    }

}